package com.tencent.user.controller;

import com.tencent.basic.exception.BusinessException;
import com.tencent.basic.util.AjaxResult;
import com.tencent.user.dto.BinderDto;
import com.tencent.user.dto.LoginDto;
import com.tencent.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 账号登录接口
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult loginAccount (@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = logininfoService.loginAccount(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException b) {
            b.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙诶");
        }
    }

    /**
     * 微信登录接口
     * @param code
     * @return
     */
    @GetMapping("/wechat/{code}")
    public AjaxResult loginWechat (@PathVariable String code) {
        try {
            return logininfoService.loginWechat(code);
        } catch (BusinessException b) {
            b.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙诶");
        }
    }

    /**
     * 微信绑定接口
     * @param binderDto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binderWechat (@RequestBody BinderDto binderDto) {
        try {
            return logininfoService.binderWechat(binderDto);
        } catch (BusinessException b) {
            b.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙诶");
        }

    }


    /**
     * 用户退出
     * @param token
     * @return
     */
    @GetMapping("/logout/{token}")
    public AjaxResult logout (@PathVariable String token) {
        try {
            logininfoService.logout(token);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙诶");
        }

    }
}
